package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.Constant;
import com.example.demo.common.SessionUtil;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2023-02-14
 * Time: 11:50
 */
@RequestMapping("/art")
@RestController
public class ArticleController {

    @Autowired
    ArticleService articleService;

    private Object locker = new Object();

    @RequestMapping("/mylist")
    public List<ArticleInfo> myList(HttpServletRequest request) {
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if(userInfo!=null) {
            return articleService.getMyList(userInfo.getId());
        }
        return null;
    }

    @RequestMapping("/myinfo")
    public UserInfo myInfo(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null) {
            UserInfo userInfo = SessionUtil.getLoginUser(request);
            return userInfo;
        }
        return null;
    }

    @RequestMapping("/del")
    public Integer artDel(HttpServletRequest request,Integer aid) {
        //非空效验
        if(aid!=null && aid>0) {
            //根据文章id查询文章详情
            ArticleInfo articleInfo = articleService.getDetail(aid);
            //进行归属人验证
            UserInfo userInfo = SessionUtil.getLoginUser(request);
            if(articleInfo!=null && userInfo!=null && userInfo.getId()!=articleInfo.getUid()) {
                //归属人正确进行删除
                int result = articleService.artDel(aid);
                return result;
            }
        }
        return null;
    }


    @RequestMapping("/detail")
    public Object getDetail(Integer aid) {
        if(aid!=null && aid>0) {
            ArticleInfo articleInfo = articleService.getDetail(aid);
            //访问量加1
            synchronized (locker) {
                int result = articleService.rcountAdd(aid,articleInfo.getRcount()+1);
                if(result!=1) {
                    return AjaxResult.fail(-1,"访问量添加失败");
                }
            }
            //返回文章详情
            return AjaxResult.success(articleInfo);
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/detailbyid")
    public Object getDetailById(HttpServletRequest request,Integer aid) {
        if(aid!=null && aid>0) {
            //根据文章id查询文章详情
            ArticleInfo articleInfo = articleService.getDetail(aid);
            //文章归属人验证
            UserInfo userInfo = SessionUtil.getLoginUser(request);
            if(articleInfo!=null && userInfo!=null && articleInfo.getUid()==userInfo.getId()) {
                //文章归属人正确
                return AjaxResult.success(articleInfo);
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/update")
    public int update(HttpServletRequest request,Integer aid,String title,String content) {
        //非空效验
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content) || aid == 0 || aid<=0) {
            return 0;
        }
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if(userInfo!=null && userInfo.getId()>0) {
            return articleService.update(aid,userInfo.getId(),title,content);
        }
        return 0;
    }

    @RequestMapping("/release")
    public int releaseArt(HttpServletRequest request,String title,String content) {
        //非空效验
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return 0;
        }
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if(userInfo!=null && userInfo.getId()>0) {
            int result = articleService.releaseArt(title,content,userInfo.getId());
            if(result==1) {
                return 1;
            }
        }
        return 0;
    }

    @RequestMapping("/draft")
    public int draftArt(HttpServletRequest request,String title,String content) {
        //非空效验
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return 0;
        }
        int state = 2;//将state设置为2
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if(userInfo!=null && userInfo.getId()>0) {
            //将文章保存到数据库，并将state设置为2，表示还未发布
            int result = articleService.draftArt(title,content,userInfo.getId(),2);
            return result;
        }
            return 0;
    }

    @RequestMapping("/publish")
    public int publishArt(Integer aid) {
        if(aid!=null) {
            //将state设置为1
            int result = articleService.publishArt(aid,1);
            if(result==1) {
                return 1;
            }
        }
        return 0;
    }

    @RequestMapping("/totalpage")
    public Integer getTotalCount(Integer psize) {
        if(psize!=null) {
            //参数有效
            int totalCount = articleService.getTotalCount();
            int totalPage = (int)Math.ceil(totalCount*1.0/psize);
            return totalPage;
        }
        return null;
    }

    @RequestMapping("/list")
    public List<ArticleInfo> getList(Integer pindex,Integer psize) {
        if(pindex==null || psize==null) {
            return null;
        }
        int offset = (pindex-1)*psize;
        return articleService.getList(psize,offset);
    }
}
